package com.bbcloud.controller.portal;

import com.bbcloud.common.ServerResponse;
import com.bbcloud.service.ILinkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by liuzhu on 2018/12/5.
 */
@Controller
@RequestMapping("/link/")
public class LinkController {

    @Autowired
    private ILinkService iLinkService;


    /**
     * 发送link消息
     * @param token token
     * @param userId    userId
     * @param linkType  1.普通link，2.打卡link
     * @param file  linkType为1时,通过这个上传图片,linkType为2时,直接把day的图片url给linkImage就行了
     * @param linkImage 图片URL
     * @param description   描述
     * @param usedTime  耗时
     * @param times 次数
     * @param mileage   距离
     * @param request
     * @return
     */
    @RequestMapping(value = "send.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse sendLink(@RequestParam(value = "token", required = true) String token,
                                   @RequestParam(value = "userId", required = true) String userId,
                                   @RequestParam(value = "linkType", required = true) int linkType,
                                   @RequestParam(value = "upload_file",required = false) MultipartFile file,
                                   @RequestParam(value = "linkImage", required = false) String linkImage,
                                   @RequestParam(value = "description", required =false) String description,
                                   @RequestParam(value = "usedTime", defaultValue = "0") int usedTime,
                                   @RequestParam(value = "times", defaultValue = "0") int times,
                                   @RequestParam(value = "mileage", defaultValue = "0") int mileage,
                                   HttpServletRequest request)
    {

        String path = request.getSession().getServletContext().getRealPath("upload");
        return iLinkService.sendLink(token,userId,linkType,file,linkImage,path,description,usedTime,times,mileage);
    }

    @RequestMapping(value = "list.do",method = RequestMethod.POST)
    @ResponseBody
    public  ServerResponse list(@RequestParam(value = "token", required = true) String token,
                                @RequestParam(value = "userId", required = true) String userId,
                                @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                @RequestParam(value = "orderBy", defaultValue = "") java.lang.String orderBy)
    {
        return iLinkService.list(token,userId,pageNum,pageSize,orderBy);
    }

}
